package me.zhengjie.modules.system.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.service.SafeTestTypeService;
import me.zhengjie.modules.system.service.dto.RecommendParam;
import me.zhengjie.modules.system.service.dto.SafeRiskTypeDto;
import me.zhengjie.modules.system.service.dto.SafeRiskTypeQueryCriteria;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * SafeRiskTypeController
 *
 * @author guoyuan
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "安全规则：场景风险类别")
@RequestMapping("/api/safeTestType")
public class SafeTestTypeController {

    private final SafeTestTypeService safeTestTypeService;

    @Log("新增风险类别")
    @ApiOperation("新增风险类别")
    @PostMapping("/create")
    @PreAuthorize("@el.check('testType:create')")
    public ResponseEntity<Object> create(@Validated @RequestBody List<SafeRiskTypeDto> resources, @RequestParam String parentId) {
        if (resources == null || resources.isEmpty()) {
            throw new BadRequestException("Request body is null or empty");
        }
        safeTestTypeService.create(resources, parentId);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("删除风险类别")
    @ApiOperation("删除风险类别")
    @DeleteMapping("/delete")
    @PreAuthorize("@el.check('testType:delete')")
    public ResponseEntity<Object> delete(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            throw new BadRequestException("SafeRiskType id is null or empty");
        }
        safeTestTypeService.delete(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("修改风险类别")
    @ApiOperation("修改风险类别")
    @PutMapping("/edit")
    @PreAuthorize("@el.check('testType:edit')")
    public ResponseEntity<Object> edit(@RequestBody SafeRiskTypeDto riskTypeDto, @RequestParam String parentId) {
        if (Objects.isNull(riskTypeDto)) {
            throw new BadRequestException("SafeRiskType is null or empty");
        }
        safeTestTypeService.edit(riskTypeDto, parentId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("所有风险类别列表查询")
    @GetMapping(value = "/query")
    @PreAuthorize("@el.check('testType:query')")
    public ResponseEntity<Object> query(SafeRiskTypeQueryCriteria criteria) throws Exception {
        return new ResponseEntity<>(safeTestTypeService.queryAll(criteria), HttpStatus.OK);
    }

    @Log("上传文件")
    @ApiOperation("上传文件")
    @PostMapping("/readExcel")
    @PreAuthorize("@el.check('testType:readExcel')")
    public ResponseEntity<Object> readExcel(@RequestParam("file") MultipartFile file) throws Exception {
        safeTestTypeService.upload(file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("获取Shuffle规则")
    @PostMapping(value = "/shuffle")
    @PreAuthorize("@el.check('testType:shuffle')")
    public ResponseEntity<Object> shuffle(@RequestBody List<SafeRiskTypeDto> selects) {
        return new ResponseEntity<>(safeTestTypeService.getShuffleRules(selects), HttpStatus.OK);
    }

    @ApiOperation("获取标签列表")
    @GetMapping(value = "/tags")
    @PreAuthorize("@el.check('testType:tags')")
    public ResponseEntity<Object> tags() {
        return new ResponseEntity<>(safeTestTypeService.queryTags(), HttpStatus.OK);
    }

    @ApiOperation("智能推荐")
    @GetMapping(value = "/recommend")
    @PreAuthorize("@el.check('testType:recommend')")
    public ResponseEntity<Object> recommend(@Validated RecommendParam param,
                                            @PageableDefault(sort = {"caseId"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return new ResponseEntity<>(safeTestTypeService.recommendItem(param, pageable), HttpStatus.OK);
    }
}
